package com.yzt.zhuju.webview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.utils.ViewUtils;
import com.yzt.p_base.utils.AppInfoUtils;
import com.yzt.p_base.utils.LogDataUtils;
import com.yzt.p_base.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

/**
 * author : bob
 * date   : 2022/12/9
 * desc   :
 */
public class BaseWebView extends RelativeLayout {

    private WebView mWebView;

    private Map<String, String> mHeader;

    private XysJavaScriptInterface xysJavaScriptInterface;

    public BaseWebView(Context context) {
        super(context);
        init();
    }

    public BaseWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BaseWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public WebView getWebView() {
        return mWebView;
    }

    public Map<String, String> getHeader() {
        return mHeader;
    }

    public void loadUrl(String url) {
        loadUrl(url, null);
    }

    public void loadUrl(String url, String headGson) {
        if (!StringUtils.isEmpty(headGson)) {
            mHeader = JSON.parseObject(headGson, Map.class);
        }

        if (mHeader == null) {
            mHeader = new HashMap<>();
        }

        // 传递状态栏高度
        mHeader.put("androidStatusBarHeight", String.valueOf(ViewUtils.getStatusBarHeight(getContext())));

        if (mHeader == null) {
            LogDataUtils.d("webview_url", "url:" + url);
            mWebView.loadUrl(url);
        } else {
            LogDataUtils.d("webview_url", "url:" + url + "\nheader:" + mHeader);
            mWebView.loadUrl(url, mHeader);
        }
    }

    public void init() {
        mWebView = new WebView(AppConfig.context);
        //设置webview的背景颜色透明
        mWebView.setBackgroundColor(0);
        mWebView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(mWebView);
        // 得到设置属性的对象
        WebSettings webSettings = mWebView.getSettings();
        String ua = webSettings.getUserAgentString();
        webSettings.setUserAgentString(ua + ";xys_app;" + AppInfoUtils.getAppPackageName() + ";" + AppInfoUtils.getAppMetaData("UMENG_CHANNEL"));

        LogDataUtils.d("WebView", "UserAgent == " + webSettings.getUserAgentString());
        // 使能JavaScript
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        //启用数据库
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccess(true);
        //设置定位的数据库路径
        String dir = AppConfig.context.getDir("database", MODE_PRIVATE).getPath();
        webSettings.setGeolocationDatabasePath(dir);
        //开启DomStorage缓存
        webSettings.setDomStorageEnabled(true);
        webSettings.setGeolocationEnabled(true);
        //设置此属性，可任意比例缩放
        webSettings.setUseWideViewPort(true);
        webSettings.setDomStorageEnabled(true);
       // webSettings.setAppCacheEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        xysJavaScriptInterface = new XysJavaScriptInterface(getContext(), mWebView);
        mWebView.addJavascriptInterface(xysJavaScriptInterface, "xys");
    }

    public void onDestroy() {
        if (xysJavaScriptInterface != null) {
            xysJavaScriptInterface.onDerstoy();
        }
    }
}

